<?php
namespace app\index\model;

use think\Model;
use think\facade\Request;

class Logs extends Model
{

    // 获取指定用户的所有对话聊天记录
    public function getAllList($uid) {
        return $this->where(['user_id'=>$uid])->select();
    }

    // 提问远程获取内容
    public function getGpt($desc) {
      $api_key = config('apiKey');
      $curl = curl_init();
      curl_setopt_array($curl, [
          CURLOPT_URL => 'https://api.openai.com/v1/completions',
          // CURLOPT_RETURNTRANSFER => true,
          CURLOPT_POST => true,
          CURLOPT_HTTPHEADER => [
              'Content-Type: application/json',
              'Authorization: Bearer ' . $api_key // 
          ],
          CURLOPT_POSTFIELDS => json_encode([
              "model" => "text-davinci-003",
              "prompt" => $desc,
              "temperature" => 0.7,
              "max_tokens" => 4000,
              "top_p" => 1,
              "frequency_penalty" => 0,
              "presence_penalty" => 0
          ])
      ]);
      $response = curl_exec($curl);
      curl_close($curl);
  
      return $response;
    }


}